﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modules.Commander.Repositories;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Exceptions;
using System.ComponentModel.Design;

namespace Modules.Commander
{
    [UseDI(ServiceLifetime.Scoped)]
    public class CommanderProjectService
    {
        private readonly IApplicationContext applicationContext;
        private readonly CommanderProjectRepository commanderProjectRepository;
        private readonly ILogger logger;

        public CommanderProjectService(IApplicationContext applicationContext, CommanderProjectRepository commanderProjectRepository, ILogger<CommanderService> logger)
        {
            this.applicationContext = applicationContext;
            this.commanderProjectRepository = commanderProjectRepository;
            this.logger = logger;
        }

        public void CreateOrUpdate(int commanderId, IEnumerable<CommanderProject> inCommanderProjects)
        {
            commanderProjectRepository.CreateOrUpdate(commanderId, inCommanderProjects);
        }

        public void InsertCommanderProject(CommanderProject commanderProject)
        {
            IEnumerable<CommanderProject> commanders = commanderProjectRepository.GetCommanderProjectsByCommanderId(commanderProject.CommanderId);
            if (!commanders.Any(i => i.ProjectId == commanderProject.ProjectId))
            {
                commanderProjectRepository.Insert(commanderProject);
            }
        }

        public IEnumerable<CommanderProject> GetCommanderProjectsByCommanderId(int CommanderId)
        {
            return commanderProjectRepository.GetCommanderProjectsByCommanderId(CommanderId);
        }

        public void DeletedCommanderProject(CommanderProject commanderProject)
        {
            commanderProjectRepository.Delete(commanderProject);
        }

        public bool ExistsCommanderProject(int projectId, int commanderId)
        {
            return commanderProjectRepository.ExistsCommanderProject(projectId, commanderId);
        }
    }
}